#pragma once

#include "Structure.h"

class LibraryRuleParser
{
public:
	LibraryRuleParser();
	bool Compile(LPCTSTR inputText,CNamesPtrArray& labels);
	bool Verify(LPCTSTR inputText,CNamesPtrArray& labels);

	CStringArray ruleLabels;
	CString resultSQLText;
	CString errorMessage;
private:
	enum ParserTokenType
	{
		INTERSECT,UNION,EXCEPT,LPARENT,RPARENT,LABEL
	};

	struct ParserToken 
	{
		ParserTokenType type;
		CString str;
	};


	CArray<ParserToken> tokens;

	LibraryRuleParser(const LibraryRuleParser&);
	LibraryRuleParser& operator=(const LibraryRuleParser&);

	CMap<CString,LPCTSTR,ParserTokenType,ParserTokenType&> tokenMap;

	int bexp(int index);
	int bterm(int index);
	int factor(int index);
	CString subSQLText;

	bool Segment(CString rule);
	bool Parse(CNamesPtrArray& correctLabel);
	bool TransformToSQLText();
};